﻿/* 程序：KyRun
    程序：KyRun
    作用：整合AltRun+Everything+AnyTXT搜索
    作者：Kawvin(285781427@qq.com)
    版本：0.4
    AHK版本：1.1.33.10
    测试环境：Win10
    使用方法：
		1.使用F1-->AnyTxt搜索，
			F3：带引号精确搜索，否则模糊搜索
			F4-F9：选择在C-Z盘，只能是单选（因为AnyTxt的限制）
			输入内容后，按【Enter】或【数字键盘Enter】或【双击】执行
		2.使用F2-->Everything搜索，
			F4-F9：选择在C-Z盘，多选
			输入内容后，按【Enter】或【数字键盘Enter】或【双击】执行
		3.AltRun方式搜索
			a) 在参数里不带【xxx】参数的，如果在ini里设置【仅一项时执行】，当只有一项时直接执行；否则需要，按【Enter】或【数字键盘Enter】或【双击】执行
			b) 在参数里带【xxx】参数的，如百度，则输入【bd 查找内容】，则进行百度搜索，会按空格前面的内容查找匹配的命令；如果有多个命令的，可以【上键/下键】选择命令后，按【Enter】或【数字键盘Enter】或【双击】执行
			c) 如果命令行里带{%c}的，则自动替换为剪贴板内的内容，如【bc】命令，则自动将http://www.baidu.com/s?wd={%c}，替换为http://www.baidu.com/s?wd=编码的剪贴板内容
			d) 如果命令行里带{%p}的，则精确进行输入内容的匹配，
				配置文件为：	F0  |No_Encoding  |any    |AnyTxt   |d:\Program Files\AnyTXT Searcher\ATGUI.exe /d e:\数据检索\ /s """{%p}"""
				输入文字为：	any  这是一个测试数据
				执行命令为：	d:\Program Files\AnyTXT Searcher\ATGUI.exe /d e:\数据检索\ /s """这是一个测试数据"""
		4.传参数使用
			Kyrun.ahk aPara bPara
			aPara：搜索内容
			bPara：1=AnyTXT搜索，2=Everything搜索
			如：打开界面，以AnyTXT搜索，调用示例为：d:\QuickRun\Kyrun.ahk 这是要查找的内容 1
			       打开界面，以Everything搜索，调用示例为：d:\QuickRun\Kyrun.ahk 这是要查找的内容 2
				   打开界面，以AltRun调用，调用示例为：d:\QuickRun\Kyrun.ahk  这是要查找的内容
	版本说明：
		【V0.4_2022.12.04】：
			托盘菜单添加【开机启动】功能。
		【V0.3_2022.12.03】：
			a) 增加{%p}参数，用来精确进行输入内容的匹配，如
				配置文件为：	F0  |No_Encoding  |any    |AnyTxt   |d:\Program Files\AnyTXT Searcher\ATGUI.exe /d e:\数据检索\ /s """{%p}"""
				输入文字为：	any  这是一个测试数据
				执行命令为：	d:\Program Files\AnyTXT Searcher\ATGUI.exe /d e:\数据检索\ /s """这是一个测试数据"""
			b) 增加托盘右键菜单
			c) 增加拖运文件到主窗体，自动打开快捷项添加窗口功能（感谢【sunwind（刘玉宽）】的建议）
			d) 增加如果查找不到匹配项后，按【Enter】或【数字键盘Enter】或【双击】自动执行Everything搜索功能（感谢【sunwind（刘玉宽）】的建议）
		【V0.2_2022.12.02】：
			a) 增加了AnyTxt搜索和Everything搜索记住默认盘符的功能，感谢【天甜】的建议
			b) 增加了双击选中项执行的功能，感谢【sunwind（刘玉宽）】的建议
			c) 修正了{%c}剪贴板的运行错误
		【V0.1_2022.12.01】：第1版，初试
	感谢：抄袭了【ET民工】的AltRun，借鉴了【万年书妖】的WalaAltrun，感谢【天甜】的测试和建议，感谢【sunwind（刘玉宽）】的建议
*/

Label_ScriptSetting:	;{ 脚本前参数设置
	#Persistent
	#SingleInstance force
	SetTitleMatchMode,1
	Menu Tray, Icon, %A_ScriptDir%\KyRun.ico
	Menu, Tray, NoStandard        ;自定义菜单放在标准菜单上面
	Menu, tray, add, F10查看设置,KySub_KyRun_F10
	Menu, tray, add, F11快捷项文件,KySub_KyRun_F11
	Menu, tray, add, F12快捷项添加,KySub_KyRun_F12
	Menu, tray, add ; 分隔符
	Menu, tray, add, 开机启动,TrayHandle_StartWithWindows
	Menu, tray, add, 编辑脚本,TrayHandle_Edit
	Menu, tray, add, 打赏作者,TrayHandle_打赏作者
	Menu, tray, add, 重启脚本,TrayHandle_ReLoad
	Menu, tray, add ; 分隔符
	Menu, tray, add, 退出,TrayHandle_Exit
;}

Label_DefVar:	;{ 设置主参数
	KyShortcutlist=%A_ScriptDir%\ShortCutList.txt
	KyRunini=%A_ScriptDir%\KyRun.ini
	DriveArray:={"F4":"MyVal_C","F5":"MyVal_D","F6":"MyVal_E","F7":"MyVal_F","F8":"MyVal_Y","F9":"MyVal_Z"}
	KeysArray:={"MyVal_C":"F4","MyVal_D":"F5","MyVal_E":"F6","MyVal_F":"F7","MyVal_Y":"F8","MyVal_Z":"F9"}
	IfNotExist,%KyRunini%
	{
		FileAppend,[设置]`n,%KyRunini%
		FileAppend,仅一项时执行=1`n,%KyRunini%
		FileAppend,启动热键=`n,%KyRunini%
		FileAppend,;精确匹配=设置是内容完全和输入一致还是模糊匹配`n,%KyRunini%
		FileAppend,精确匹配=0`n,%KyRunini%
		FileAppend,开机启动=0`n,%KyRunini%
		FileAppend,AnyTXTSearcher路径=`n,%KyRunini%
		FileAppend,Everything路径=`n,%KyRunini%
		FileAppend,AnyTXT搜索盘符=`n,%KyRunini%
		FileAppend,Everything搜索盘符=`n,%KyRunini%
	}
	KyNull:=""
	IniRead,仅一项时执行,%KyRunini%,设置,仅一项时执行,0
	IniRead,精确匹配,%KyRunini%,设置,精确匹配,0
	IniRead,开机启动,%KyRunini%,设置,开机启动,0
	IniRead,AnyTXTSearcher路径,%KyRunini%,设置,AnyTXTSearcher路径,%KyNull%
	IniRead,Everything路径,%KyRunini%,设置,Everything路径,%KyNull%
	IniRead,AnyTXT搜索盘符,%KyRunini%,设置,AnyTXT搜索盘符,%KyNull%
	IniRead,Everything搜索盘符,%KyRunini%,设置,Everything搜索盘符,%KyNull%
	if (开机启动=1) 
		Menu, tray, check, 开机启动
	else
		Menu, tray, Uncheck, 开机启动
;}

Label_RegHotKey: ;{ 注册热键
	IniRead,启动热键,%KyRunini%,设置,启动热键,%KyNull%
	if (启动热键="" or 启动热键="ERROR"){
		MsgBox, 48, 错误, 请设置热键！
		RunWait,%KyRunini%
		Reload
	}
	RunHotKey=$%启动热键%
	;注册热键
	hotkey,%RunHotKey%,Label_DrawGui_KyRun
	; 配合参数调用，跳过注册热键
	if  0>0
		gosub,Label_Main
return
;}

Label_Main:	;{ 运行脚本
	if  0>0
	{
		aPara=%1%
		if 2!=
			bPara=%2%
		else
			bPara:=""
	}
;}

Label_DrawGui_KyRun: ;{ KyRun主窗体
	Menu Tray, Icon, %A_ScriptDir%\KyRun.ico
	try
		Gui, KyRun:Destroy
	Gui, KyRun:+AlwaysOnTop +LastFound ;-Caption
	if (aPara!="")
		Gui, KyRun:Add, Edit, x5 y20 w420 h23 vMyVal_Search gKySub_KyRun_Search,%aPara%
	else
		Gui, KyRun:Add, Edit, x5 y20 w420 h23 vMyVal_Search gKySub_KyRun_Search,
	if (bPara=1)
		Gui, KyRun:Add, Checkbox,  section ys-9 h20 checked vMyVal_FindAnyTxt gKySub_FindAnyTxt,F1 AnyTxt
	else
		Gui, KyRun:Add, Checkbox,  section ys-9 h20  vMyVal_FindAnyTxt gKySub_FindAnyTxt,F1 AnyTxt
	if (bPara=2)	
		Gui, KyRun:Add, Checkbox,  xs yp+21 h21  checked vMyVal_FindEverything gKySub_FindEverything,F2 Everything
	else
		Gui, KyRun:Add, Checkbox,  xs yp+21 h21  vMyVal_FindEverything gKySub_FindEverything,F2 Everything
	Gui, KyRun:Add, Checkbox,  checked section x5 y55 h23  vMyVal_Exact ,带引号精确搜索F3
	Gui, KyRun:Add, Checkbox, ys  h23 vMyVal_C gKySub_KyRun_F4_F9,C盘F4
	Gui, KyRun:Add, Checkbox, ys  h23 vMyVal_D gKySub_KyRun_F4_F9,D盘F5
	Gui, KyRun:Add, Checkbox, ys  h23 vMyVal_E gKySub_KyRun_F4_F9,E盘F6
	Gui, KyRun:Add, Checkbox, ys  h23 vMyVal_F gKySub_KyRun_F4_F9,F盘F7
	;~ Gui, KyRun:Add, Checkbox, ys  h23 vMyVal_G gKySub_KyRun_F4_F9,G盘F7
	Gui, KyRun:Add, Checkbox, ys  h23 vMyVal_Y gKySub_KyRun_F4_F9,Y盘F8
	Gui, KyRun:Add, Checkbox, ys  h23 vMyVal_Z gKySub_KyRun_F4_F9,Z盘F9
	Gui, KyRun:Add, Button,ys w23 h23 gTrayHandle_打赏作者,❤
	Gui, KyRun:Font, s11
	Gui, KyRun:Add, ListView, r11 w540 xs Count 20 -Hdr  NoSort gKySub_ListView,11111|222222222222222|333333333333333333333333333333333333333333|44444
	Gui, KyRun:Font
	;~ Gui, KyRun:Add, Button, ys ,Enterw
	gosub,KySub_KyRun_FillListView
	Gui,KyRun:Show, w555 h305,KyRun`n（F12快捷项添加  F11快捷项文件  F10查看设置）
	
	;配合参数调用-直接查找
	if  0>0
		gosub,KySub_KyRun_Search
	
	;切换输入法
	Sleep,50
	;~ en := DllCall("LoadKeyboardLayout", "Str", "00000409", "Int", 1)
	;~ msgbox % en
	cn := DllCall("LoadKeyboardLayout", "Str", "00000804", "Int", 1)
	;~ msgbox % cn
	PostMessage, 0x50,0, %cn%,, A
return
;}

KySub_KyRun_FillListView: ;{ 填充ListView数据
	gosub,KySub_KyRun_LoadData
	Gui,KyRun:Default
	GuiControl, -Redraw, SysListView321  ; 在加载时禁用重绘来提升性能
	isbreak := false
	LV_Delete()
	iIndex:=0
	KeyIndex:=0
	for k,v in ShortcutsObj
	{
		iIndex+=1
		KeyIndex+=1
		if (v[2]!=""){
			LV_Add("",iIndex,v[3],v[4] . "【" . v[2] . "】",KeyIndex)
		} else {
			LV_Add("",iIndex,v[3],v[4],KeyIndex)
		}
		if (iIndex>=10)
			break
	}
	GuiControl, +Redraw, SysListView321
	LV_ModifyCol(1, "Integer sort Right")  ;立即对第一列按升序排列
	LV_Modify(1, "Select Focus")  ;选择第一行
return
;}

KySub_KyRun_LoadData: ;{ 从ShortCutList.txt加载数据到Ojb
	FileRead,ALLText,%KyShortcutlist%
	sort,ALLText,R N P2
	;~ msgbox % ALLText
	ShortcutsObj:={}
	Loop,parse,ALLText,`n,`r
	{
		if (A_LoopField="")
			continue
		tArray:=strsplit(A_LoopField,"|"," `t")
		ShortcutsObj[A_Index]:=tArray
	}

return
;}

KySub_KyRun_Search: ;{ 根据输入内容进行筛选
	GuiControlGet,MyVal_FindAnyTxt,KyRun:,MyVal_FindAnyTxt
	GuiControlGet,MyVal_FindEverything,KyRun:,MyVal_FindEverything
	if(MyVal_FindEverything+MyVal_FindAnyTxt>0){
		;~ Gui,KyRun:Default
		;~ LV_Delete()
		return
	}
	Critical  ;防止当前线程被其他线程中断。
	Gui,KyRun:Default
	GuiControl, -Redraw,SysListView321
	isbreak := true
	Gui, Submit , NoHide   ;将编辑框的内容保存到v变量名中（AltrunInputContent）
	if (instr(MyVal_Search," "))=0 {
		Real_Search:=MyVal_Search
	} else {
		Real_Search:=substr(MyVal_Search,1,instr(MyVal_Search," ")-1)
	}
	LV_Delete()
	iIndex:=0
	KeyIndex:=0
	for k,v in ShortcutsObj
	{
		KeyIndex+=1
		if 精确匹配 {
			if ( instr(v[3],Real_Search)=1){
				iIndex+=1
				if (v[2]!=""){
					LV_Add("",iIndex,v[3],v[4] . "【" . v[2] . "】",KeyIndex)
				} else {
					LV_Add("",iIndex,v[3],v[4],KeyIndex)
				}
			}
		} else {
			if ( instr(v[3],Real_Search)>=1){
				iIndex+=1
				if (v[2]!=""){
					LV_Add("",iIndex,v[3],v[4] . "【" . v[2] . "】",KeyIndex)
				} else {
					LV_Add("",iIndex,v[3],v[4],KeyIndex)
				}
			}
		}
		if (iIndex>=10)
			break

	}
	if  (仅一项时执行 && iIndex=1){
		LV_GetText(KeyIndex,1,4)
		;~ msgbox % KeyIndex
		RunArray:=ShortcutsObj[KeyIndex]
		if (RunArray[2]="")
			gosub,KyRunButtonEnter
	}
	GuiControl, +Redraw, SysListView321
	Critical,off
	LV_ModifyCol(1, "Integer sort Right")  ;立即对第一列按升序排列
	LV_Modify(1, "Select Focus")  ;选择第一行
return
;}

KyRunButtonEnter: ;{ 执行操作
	Gui,KyRun:Submit,NoHide
	if (MyVal_FindAnyTxt=1){ ;使用AnyTXT 搜索内容
		Para:=""
		if (MyVal_C=1)
			Para:=" /d C: "
		if (MyVal_D=1)
			Para.=" /d D:"
		if (MyVal_E=1)
			Para.=" /d E:"
		if (MyVal_F=1)
			Para:=" /d F: "
		if (MyVal_G=1)
			Para:=" /d G: "
		if (MyVal_Y=1)
			Para:=" /d Y: "
		if (MyVal_Z=1)
			Para:=" /d Z: "
		if (MyVal_Search=""){
			Run,"%AnyTXTSearcher路径%"
		} else {
			if (MyVal_Exact=1)
				MyVal_Search="""%MyVal_Search%"""
			else
				MyVal_Search="%MyVal_Search%"
			MyCmdLine="%AnyTXTSearcher路径%" /s %MyVal_Search% %Para%
			;~ msgbox % "AnyTxt--" MyCmdLine
			Run,%MyCmdLine%
		}
	} 
	else if (MyVal_FindEverything=1){  ;使用Everything 搜索内容
		Para:=""
		if (MyVal_C=1)
			Para.="C:|"
		if (MyVal_D=1)
			Para.="D:|"
		if (MyVal_E=1)
			Para.="E:|"
		if (MyVal_F=1)
			Para.="F:|"
		if (MyVal_G=1)
			Para.="G:|"
		if (MyVal_Y=1)
			Para.="Y:|"
		if (MyVal_Z=1)
			Para.="Z:|"
		if (Para!="")
			Para:= "<" . substr(Para,1,StrLen(Para)-1) . ">"
		if (MyVal_Search=""){
			Run,"%Everything路径%"
		} else {
			if (MyVal_Exact=1)
				MyVal_Search="""%MyVal_Search%"""
			else
				MyVal_Search="%MyVal_Search%"
			MyCmdLine="%Everything路径%"  -search %Para%%MyVal_Search% 
			;~ msgbox % "EV--" MyCmdLine
			Run,%MyCmdLine%
		}
	} 
	else {
		; 其他相关的操作
		Gui,KyRun:Default
		if (LV_GetCount()=0) { ; 如果没有任何匹配项，直接Everything查找
			if (Everything路径="" || !FileExist(Everything路径)){
				gosub,KyRunGuiClose
				MsgBox, 48, 错误, 请设置Everything路径！
				RunWait,%KyRunini%
				Reload
			}
			MyCmdLine="%Everything路径%"  -search %MyVal_Search% 
			;~ msgbox % "NoMatch_EV--" MyCmdLine
			Run,%MyCmdLine%
		} else  {
			FocusedRowNumber := LV_GetNext(0, "F")  ; 查找焦点行.
			if not FocusedRowNumber  ; 没有焦点行.
				FocusedRowNumber=1
			LV_GetText(KeyIndex,FocusedRowNumber,4)
			RunArray:=ShortcutsObj[KeyIndex]
			;~ if (RunArray[2]="")
			if (instr(MyVal_Search," "))=0 {
				Real_Search:=""
			} else {
				Real_Search:=trim(substr(MyVal_Search,instr(MyVal_Search," ")+1))
			}
			switch RunArray[2]
			{
				case "UTF8_Query":
					Real_Search:=KySub_UrlEncode(trim(Real_Search),"UTF-8")
				case "URL_Query ":
					Real_Search:=KySub_UrlEncode(trim(Real_Search),"CP0")
				case "No_Encoding":
					Real_Search:=Real_Search
				Default:
					Real_Search:=Real_Search
			}
			ExePath:=RunArray[5]
			;特殊字符串替换--相对路径
			ExePath:=RegExReplace(ExePath,"^\.\\", A_ScriptDir . "\")
			;特殊字符串替换--剪贴板
			if (instr(ExePath,"{%c}")>0) {
				switch % RunArray[2]
				{
					case "UTF8_Query":
						Coding_Clipboard:=KySub_UrlEncode(Clipboard,"UTF-8")
					case "URL_Query ":
						Coding_Clipboard:=KySub_UrlEncode(Clipboard,"CP0")
					case "No_Encoding":
						Coding_Clipboard:=Clipboard
					Default:
						Coding_Clipboard:=Clipboard
				}
				ExePath:=StrReplace(ExePath,"{%c}",Coding_Clipboard)
				if (substr(ExePath,0)="=")
					MyCmdLine=%ExePath%%Real_Search% 
				else
					MyCmdLine=%ExePath% %Real_Search% 
			} 
			else if (instr(ExePath,"{%p}")>0) { ;特殊字符串替换--精确替换需要
				ExePath:=StrReplace(ExePath,"{%p}",Real_Search)
				MyCmdLine=%ExePath%
			}
			else {
				if (substr(ExePath,0)="=")
					MyCmdLine=%ExePath%%Real_Search% 
				else
					MyCmdLine=%ExePath% %Real_Search% 
			}
			;~ msgbox % "其他--" MyCmdLine
			Run,%MyCmdLine%	
			KySub_WriteObj2Txt(KeyIndex)
		}
	}
	gosub,KyRunGuiClose
return
;}

KySub_ListView: ;{ ListView控件 添加功能：双击选中项执行
	if (A_GuiEvent="DoubleClick"){
		gosub,KyRunButtonEnter
	}
return
;}

KySub_UrlEncode(str, enc:="UTF-8"){ ; 文本编码
	hex := "00"
	fun := "msvcrt\swprintf"
	VarSetCapacity(buff, size:=StrPut(str, enc))
	StrPut(str, &buff, enc)
	while(code:=NumGet(buff, A_Index - 1, "UChar")) && dllcall(fun, "str",hex, "str","%%%02X", "uchar",code, "cdecl")
		r .= hex
	return r
}

KySub_WriteObj2Txt(aIndex){ ; 写入到ShortCutList.txt
	global ShortcutsObj
	global KyShortcutlist
	NewAllTxt:=""`
	for k,v in ShortcutsObj
	{
		if (k=aIndex){
			NCount:="F" . substr(v[1],2)+1
			NewAllTxt.= NCount "|" v[2] "|" v[3] "|" v[4] "|" v[5] "`n"
		} else {
			NewAllTxt.= v[1] "|" v[2] "|" v[3] "|" v[4] "|" v[5] "`n"
		}
	}
	NewAllTxt:=KySub_AlignString(NewAllTxt)
	FileDelete,%KyShortcutlist%
	FileAppend,%NewAllTxt%`n,%KyShortcutlist%
}

KySub_AlignString(aString,SplitStr:="|"){ ; 对文本进行对齐操作
	;~ SplitStr:="|"
	ListArray:=[]
	MaxArray:=[]
	StrSpace:="                                                                                                                                                                                                                            "
	loop, parse, aString, `n,`r                   ;首先求得左边最长的长度，以便向它看齐
	{
		tArray:=[]
		If(trim(A_loopfield)="")  
			Continue
		tArray:=StrSplit(A_LoopField,SplitStr)
		loop,% tArray.length()
		{
			MaxArray[A_index]:=strlen(trim(tArray[A_index]))>MaxArray[A_index]?strlen(trim(tArray[A_index])):MaxArray[A_index]
		}
		ListArray.push(tArray)
	}
	Aligned:=""
	loop,%  ListArray.length()
	{
		aArray:=ListArray[A_Index]
		loop,% aArray.length()-1
			Aligned:=Aligned . substr(trim(aArray[A_Index]) . StrSpace,1,MaxArray[A_Index]+2) . SplitStr
		Aligned:=Aligned . trim(aArray[aArray.length()]) . "`n"
	}
	Aligned:=RegExReplace(Aligned,"\s*$","")   ;顺便删除最后的空白行，可根据需求注释掉
	return, Aligned
}

#IfWinActive KyRun ahk_class AutoHotkeyGUI   ;{  定义窗体内Space热键	
	Enter::
	NumpadEnter:: ;{ 进行查找
		gosub,KyRunButtonEnter
	return
;}

	KySub_FindAnyTxt: ;{ 内容查找-AnyTxt
		if (AnyTXTSearcher路径="" || !FileExist(AnyTXTSearcher路径)){
			gosub,KyRunGuiClose
			MsgBox, 48, 错误, 请设置AnyTXTSearcher路径！
			RunWait,%KyRunini%
			Reload
		}
		GuiControlGet,MyVal_FindAnyTxt,KyRun:,MyVal_FindAnyTxt
		if (MyVal_FindAnyTxt=1){
			GuiControl,KyRun:,MyVal_FindEverything,0
			for k,v in DriveArray
			{	
				LoopDrive:=v
				if (substr(LoopDrive,0)=AnyTXT搜索盘符)
					GuiControl,KyRun:,%LoopDrive%,1
				else
					GuiControl,KyRun:,%LoopDrive%,0
			}
		} else {
			for k,v in DriveArray
			{	
				LoopDrive:=v
				GuiControl,KyRun:,%LoopDrive%,0
			}
		}
	return
	;}
	
	F1:: ;{ 内容查找-AnyTxt
		if (AnyTXTSearcher路径="" || !FileExist(AnyTXTSearcher路径)){
			gosub,KyRunGuiClose
			MsgBox, 48, 错误, 请设置AnyTXTSearcher路径！
			RunWait,%KyRunini%
			Reload
		}
		GuiControlGet,MyVal_FindAnyTxt,KyRun:,MyVal_FindAnyTxt
		if (MyVal_FindAnyTxt=1){
			GuiControl,KyRun:,MyVal_FindAnyTxt,0
			for k,v in DriveArray
			{	
				LoopDrive:=v
				GuiControl,KyRun:,%LoopDrive%,0
			}
		} else {
			GuiControl,KyRun:,MyVal_FindAnyTxt,1
			GuiControl,KyRun:,MyVal_FindEverything,0
			for k,v in DriveArray
			{	
				LoopDrive:=v
				if (substr(LoopDrive,0)=AnyTXT搜索盘符)
					GuiControl,KyRun:,%LoopDrive%,1
				else
					GuiControl,KyRun:,%LoopDrive%,0
			}
		}
		
	return
;}
	
	KySub_FindEverything: ;{ 内容查找-Everything
		if (Everything路径="" || !FileExist(Everything路径)){
			gosub,KyRunGuiClose
			MsgBox, 48, 错误, 请设置Everything路径！
			RunWait,%KyRunini%
			Reload
		}
		GuiControlGet,MyVal_FindEverything,KyRun:,MyVal_FindEverything
		if (MyVal_FindEverything=1){
			GuiControl,KyRun:,MyVal_FindAnyTxt,0
			for k,v in DriveArray
			{	
				LoopDrive:=v
				if (instr(Everything搜索盘符,substr(LoopDrive,0))>0)
					GuiControl,KyRun:,%LoopDrive%,1
				else
					GuiControl,KyRun:,%LoopDrive%,0
			}
		} else {
			for k,v in DriveArray
			{	
				LoopDrive:=v
				GuiControl,KyRun:,%LoopDrive%,0
			}
		}
	return
;}
	
	F2:: ;{ 内容查找-Everything
		if (Everything路径="" || !FileExist(Everything路径)){
			gosub,KyRunGuiClose
			MsgBox, 48, 错误, 请设置Everything路径！
			RunWait,%KyRunini%
			Reload
		}
		GuiControlGet,MyVal_FindEverything,KyRun:,MyVal_FindEverything
		if (MyVal_FindEverything=1){
			GuiControl,KyRun:,MyVal_FindEverything,0
			for k,v in DriveArray
			{	
				LoopDrive:=v
				GuiControl,KyRun:,%LoopDrive%,0
			}
		} else {
			GuiControl,KyRun:,MyVal_FindEverything,1
			GuiControl,KyRun:,MyVal_FindAnyTxt,0
			for k,v in DriveArray
			{	
				LoopDrive:=v
				if (instr(Everything搜索盘符,substr(LoopDrive,0))>0)
					GuiControl,KyRun:,%LoopDrive%,1
				else
					GuiControl,KyRun:,%LoopDrive%,0
			}
		}
	return
;}
	
	KySub_KyRun_F3:
	F3:: ;{ 精确搜索
		GuiControlGet,MyVal_Exact,KyRun:,MyVal_Exact
		NewVal:=!MyVal_Exact
		GuiControl,KyRun:,MyVal_Exact,%NewVal%
	return
;}
	
	KySub_KyRun_F4_F9: ;{ 查找C盘-Z盘
		if (A_GuiControl!="") {
			aA_ThisHotkey:=KeysArray[A_GuiControl]
			SelDrive:=DriveArray[aA_ThisHotkey]
			;~ msgbox % SelDrive
			GuiControlGet,MyVal_FindAnyTxt,KyRun:,MyVal_FindAnyTxt
			if (MyVal_FindAnyTxt=1){
				GuiControlGet,%SelDrive%,KyRun:,%SelDrive%
				AnyTXT搜索盘符:=substr(DriveArray[aA_ThisHotkey],0)
				IniWrite,%AnyTXT搜索盘符% ,%KyRunini%,设置,AnyTXT搜索盘符
				if (%SelDrive%=1) {
					for k,v in DriveArray
					{	
						LoopDrive:=DriveArray[k]
						;~ msgbox % LoopDrive
						if (k!=aA_ThisHotkey)
							GuiControl,KyRun:,%LoopDrive%,0
					}
				}
			} else {
				GuiControlGet,%SelDrive%,KyRun:,%SelDrive%
				SelEverything搜索盘符:=substr(A_GuiControl,0)
				if (%SelDrive%=1){
					if (instr(Everything搜索盘符,SelEverything搜索盘符)=0) {
						Everything搜索盘符:=Everything搜索盘符 . SelEverything搜索盘符
						IniWrite,%Everything搜索盘符% ,%KyRunini%,设置,Everything搜索盘符
					}
				} else {
					if (instr(Everything搜索盘符,SelEverything搜索盘符)>0) {
						Everything搜索盘符:=StrReplace(Everything搜索盘符,SelEverything搜索盘符)
						IniWrite,%Everything搜索盘符% ,%KyRunini%,设置,Everything搜索盘符
					} 
				}
			}
		}
	return
;}
	
	F4::
	F5::
	F6::
	F7::
	F8::
	F9:: ;{ 查找C盘-Z盘
		aA_ThisHotkey:=A_ThisHotkey
		SelDrive:=DriveArray[aA_ThisHotkey]
		;~ msgbox % SelDrive
		GuiControlGet,MyVal_FindAnyTxt,KyRun:,MyVal_FindAnyTxt
		if (MyVal_FindAnyTxt=1){
			GuiControlGet,%SelDrive%,KyRun:,%SelDrive%
			if (%SelDrive%=1) {
				GuiControl,KyRun:,%SelDrive%,0
			} else {
				for k,v in DriveArray
				{	
					LoopDrive:=DriveArray[k]
					;~ msgbox % LoopDrive
					if (k!=aA_ThisHotkey) {
						GuiControl,KyRun:,%LoopDrive%,0
					} else {
						GuiControl,KyRun:,%LoopDrive%,1
						AnyTXT搜索盘符:=substr(DriveArray[aA_ThisHotkey],0)
						IniWrite,%AnyTXT搜索盘符% ,%KyRunini%,设置,AnyTXT搜索盘符
					}
				}
			}
		} else {
			GuiControlGet,%SelDrive%,KyRun:,%SelDrive%
			NewValue:=!%SelDrive%
			GuiControl,KyRun:,%SelDrive%,%NewValue%
			SelEverything搜索盘符:=substr(DriveArray[aA_ThisHotkey],0)
			if (%NewValue%=1){
				if (instr(Everything搜索盘符,SelEverything搜索盘符)=0) {
					Everything搜索盘符:=Everything搜索盘符 . SelEverything搜索盘符
					IniWrite,%Everything搜索盘符% ,%KyRunini%,设置,Everything搜索盘符
				}
			} else {
				if (instr(Everything搜索盘符,SelEverything搜索盘符)>0) {
					Everything搜索盘符:=StrReplace(Everything搜索盘符,SelEverything搜索盘符)
					IniWrite,%Everything搜索盘符% ,%KyRunini%,设置,Everything搜索盘符
				} 
			}
		}
	return
;}
	
	KySub_KyRun_F10:
	F10:: ;{ 查看配置文件
		RunWait,%KyRunini%
		Reload
	return
;}
	
	KySub_KyRun_F11:
	F11:: ;{ 查看快捷项设置
		RunWait,%KyShortcutlist%
		Reload
	return
;}
	
	KySub_KyRun_F12:
	F12:: ;{ 快捷项添加
		Gui,KyRun:Hide
		gosub,Label_DrawGui_KyRun_Add
	return
;}
	
	up:: ;{ 方向键向上
		Gui,KyRun:Default
		FocusedRowNumber := LV_GetNext(0, "F")  ; 查找焦点行.
		if not FocusedRowNumber  ; 没有焦点行.
			return
		if (FocusedRowNumber=1)
			return
		FocusedRowNumber-=1
		LV_Modify(FocusedRowNumber, "Select Focus")  ;选择第一行
	return
;}
	
	down:: ;{ 方向键向下
		Gui,KyRun:Default
		FocusedRowNumber := LV_GetNext(0, "F")  ; 查找焦点行.
		if not FocusedRowNumber  ; 没有焦点行.
			return
		if (FocusedRowNumber=LV_GetCount())
			return
		FocusedRowNumber+=1
		LV_Modify(FocusedRowNumber, "Select Focus")  ;选择第一行
	return
;}

#If
;}

KyRunGuiDropFiles: ;{拖动文件或文件夹到-主窗体
	MySel=%A_GuiEvent%
	Loop, parse, MySel, `n,`r
	{
		If InStr(FileExist(A_LoopField), "D")  { ;区分是否文件夹,Attrib= D ,则是文件夹
			SplitPath,A_LoopField,MyOutFileName,MyOutDir,MyOutExt,MyOutNameNoExt,MyOutDrive
			MyCommand:=A_LoopField
		} else {
			SplitPath,A_LoopField,MyOutFileName,MyOutDir,MyOutExt,MyOutNameNoExt,MyOutDrive
			MyCommand:=A_LoopField
		}
		break
	}
	Gui,KyRun:hide
	gosub ,Label_DrawGui_KyRun_Add
	GuiControl,KyRun_Add:,MyVal_Shortcut_Key,%MyOutNameNoExt%
	GuiControl,KyRun_Add:,MyVal_Shortcut_Name,%MyOutNameNoExt%
	GuiControl,KyRun_Add:,MyVal_Shortcut_Command,%MyCommand%
return
;}

KyRunGuiEscape:
KyRunGuiClose: ;{ 执行完成后操作
	aPara=
	bPara=
	try
		Gui, KyRun:Destroy
	;~ Gui,KyRun:Hide
	;~ ExitApp
return
;}

Label_DrawGui_KyRun_Add: ;{ ; KyRun添加快捷项窗体
	try
		Gui, KyRun_Add:Destroy
	Gui, KyRun_Add:New
	;~ Gui, KyRun_Add:+AlwaysOnTop ;-Caption
	Gui, KyRun_Add:Add, Text, x5 y15 h23 ,快捷项
	Gui, KyRun_Add:Add, Text, xs h23 ,名  称
	Gui, KyRun_Add:Add, Text, xs h23 ,命令行
	Gui, KyRun_Add:Add, Edit, ys-5 w440 h23 vMyVal_Shortcut_Key,
	Gui, KyRun_Add:Add, Edit, xp yp+30 w440 h23 vMyVal_Shortcut_Name,
	Gui, KyRun_Add:Add, Edit, xp yp+30 w300 h23 vMyVal_Shortcut_Command,
	Gui, KyRun_Add:Add, Button, yp-2 xp+310 w60 h27 , 文件
	Gui, KyRun_Add:Add, Button, yp xp+70 w60 h27 , 文件夹
	Gui, KyRun_Add:Add, GroupBox, section x5 y110 w395 h135 ,参数类型
	Gui, KyRun_Add:Add, Radio, xs+15  ys+15 h23  checked vMyVal_None gKySub_ParaNone,无参数
	Gui, KyRun_Add:Font,cRed 
	Gui, KyRun_Add:Add, Text, xp+180 yp+20 h23  ,支持拖动【文件/文件夹】到本窗体
	Gui, KyRun_Add:Font
	Gui, KyRun_Add:Add, Radio, xs+15  ys+45 h23 vMyVal_NoEncoding gKySub_ParaNoEncoding,参数无编码
	Gui, KyRun_Add:Add, Radio, xs+15  ys+75 h23 vMyVal_URLQuery gKySub_ParaURLQuery,对参数进行URL编码（如Baidu搜索字符串）
	Gui, KyRun_Add:Add, Radio, xs+15  ys+105 h23 vMyVal_UTF8Query gKySub_ParaUTF8Query ,对参数进行UTF-8编码（如Google搜索字符串）
	Gui, KyRun_Add:Add, Button, ys+7 w80 h35  ,测试
	Gui, KyRun_Add:Add, Button, xp yp+45 w80 h35  ,确定
	Gui, KyRun_Add:Add, Button, xp yp+45 w80 h35  ,取消
	Gui, KyRun_Add:Show, w500 h250,快捷项添加
return
;}

KyRun_AddButton测试: ;{ 按钮-测试
	Gui, KyRun_Add:Submit,NoHide
	Real_Search:=""
	if (MyVal_NoEncoding+MyVal_URLQuery+MyVal_UTF8Query>0){
		InputBox,MyTemInput,请输入测试内容,,,300,100
		if ErrorLevel
			return
		if (MyTemInput="")
			return
		if (MyVal_NoEncoding=1)
			Real_Search:=MyTemInput
		if (MyVal_URLQuery=1)
			Real_Search:=KySub_UrlEncode(trim(MyTemInput),"CP0")
		if (MyVal_UTF8Query=1)
			Real_Search:=KySub_UrlEncode(trim(MyTemInput),"UTF-8")
	}
	if (substr(MyVal_Shortcut_Command,0)="=")
		MyCmdLine=%MyVal_Shortcut_Command%%Real_Search% 
	else
		MyCmdLine=%MyVal_Shortcut_Command% %Real_Search% 
	;~ msgbox % "测试--" MyCmdLine
	Run,%MyCmdLine%	

return
;}

KyRun_AddButton确定: ;{ 按钮-确定
	Gui, KyRun_Add:Submit,NoHide
	if (MyVal_None=1)
		Encoding_Type:=""
	if (MyVal_NoEncoding=1)
		Encoding_Type:="NoEncoding"
	if (MyVal_URLQuery=1)
		Encoding_Type:="URLQuery"
	if (MyVal_UTF8Query=1)
		Encoding_Type:="UTF8Query"
	NewString=F0|%Encoding_Type%|%MyVal_Shortcut_Key%|%MyVal_Shortcut_Name%|%MyVal_Shortcut_Command%
	NewString:=KySub_AlignString(NewString)
	FileAppend,%NewString%`n,%KyShortcutlist%
	Gui,KyRun:Show
	try
		Gui, KyRun_Add:Destroy
return
;}

KyRun_AddButton取消: ;{ 按钮-取消
	Gui,KyRun:Show
	try
		Gui, KyRun_Add:Destroy
return
;}

KyRun_AddButton文件: ;{ 按钮-文件
	;选择文件
	FileSelectFile, MySelFile,S1, , 请选择文件,所有文件(*.*)
	if ErrorLevel
		return
	if MySelFile =
		return
	SplitPath,MySelFile,MyOutFileName,MyOutDir,MyOutExt,MyOutNameNoExt,MyOutDrive
	GuiControl,KyRun_Add:,MyVal_Shortcut_Key,%MyOutNameNoExt%
	GuiControl,KyRun_Add:,MyVal_Shortcut_Name,%MyOutNameNoExt%
	GuiControl,KyRun_Add:,MyVal_Shortcut_Command,%MySelFile%
return
;}

KyRun_AddButton文件夹: ;{ 按钮-文件夹
	;选择目录
	FileSelectFolder, MySelPath, ::{20d04fe0-3aea-1069-a2d8-08002b30309d}, 3,选择目录
	if ErrorLevel
		return
	if MySelPath =
		return
	SplitPath,MySelPath,MyOutFileName,MyOutDir,MyOutExt,MyOutNameNoExt,MyOutDrive
	GuiControl,KyRun_Add:,MyVal_Shortcut_Key,%MyOutNameNoExt%
	GuiControl,KyRun_Add:,MyVal_Shortcut_Name,%MyOutNameNoExt%
	GuiControl,KyRun_Add:,MyVal_Shortcut_Command,%MySelPath%
return
;}

KySub_ParaNone: ;{  单选按钮-无参数
	GuiControl,KyRun_Add:,MyVal_None,1
return
;}

KySub_ParaNoEncoding: ;{  单选按钮-无编码
	GuiControl,KyRun_Add:,MyVal_NoEncoding,1
return
;}

KySub_ParaURLQuery: ;{ 单选按钮-URL编码
	GuiControl,KyRun_Add:,MyVal_URLQuery,1
return
;}

KySub_ParaUTF8Query: ;{ 单选按钮-UFT-8编码
	GuiControl,KyRun_Add:,MyVal_UTF8Query,1
return
;}

KyRun_AddGuiDropFiles: ;{拖动文件或文件夹到-添加快捷项窗体
	MySel=%A_GuiEvent%
	Loop, parse, MySel, `n,`r
	{
		If InStr(FileExist(A_LoopField), "D")  { ;区分是否文件夹,Attrib= D ,则是文件夹
			SplitPath,A_LoopField,MyOutFileName,MyOutDir,MyOutExt,MyOutNameNoExt,MyOutDrive
		} else {
			SplitPath,A_LoopField,MyOutFileName,MyOutDir,MyOutExt,MyOutNameNoExt,MyOutDrive
		}
		GuiControl,KyRun_Add:,MyVal_Shortcut_Key,%MyOutNameNoExt%
		GuiControl,KyRun_Add:,MyVal_Shortcut_Name,%MyOutNameNoExt%
		GuiControl,KyRun_Add:,MyVal_Shortcut_Command,%A_LoopField%
		break
	}
return
;}

TrayHandle_打赏作者: ;{ 开机启动
	Gui, KyRun:hide
	try
		Gui, KyRun_Donate:Destroy
	Gui, KyRun_Donate:+AlwaysOnTop +LastFound ;-Caption
	Gui, KyRun_Donate:Add, Picture,x10 y10 ,%A_ScriptDir%\打赏作者.jpg
	Gui,KyRun_Donate:Show, ,打赏作者
return
;}

KyRun_DonateGuiEscape:
KyRun_DonateGuiClose: ;{  捐助窗体关闭
	Gui, KyRun:Show
	try
		Gui, KyRun_Donate:Destroy
return
;}

TrayHandle_StartWithWindows: ;{ 开机启动
	if (开机启动=1) {
		开机启动=0
		Menu, tray, Uncheck, 开机启动
		FileDelete,%A_Startup%\KyRun.lnk
	}else {
		开机启动=1
		Menu, tray, check, 开机启动
		FileCreateShortcut, %A_ScriptFullPath%, %A_Startup%\KyRun.lnk
	}
	IniWrite ,%开机启动%,%KyRunini%,设置,开机启动
return
;}

TrayHandle_Edit: ;{ 编辑脚本
	;~ CmdLine=D:\Program Files\AutoHotkey\SciTE\SciTE.exe %A_ScriptFullPath%
	;~ run , %CmdLine%
	IfExist,D:\Program Files\AutoHotkey\SciTE\SciTE.exe
		run , D:\Program Files\AutoHotkey\SciTE\SciTE.exe %A_ScriptFullPath%
	else
		run , notepad.exe %A_ScriptFullPath%
return
;}

TrayHandle_ReLoad: ;{ 重启
	Reload
return
;}

TrayHandle_Exit: ;{ 退出
	ExitApp
return
;}

